package his.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import his.entity.ExpenseClass;
import his.persistence.ExpenseClassMapper;
import his.service.ExpenseClassService;
import org.example.his.common.api.CommonResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class ExpenseClassServiceImpl implements ExpenseClassService {
    @Autowired
    ExpenseClassMapper expenseClassMapper;

    //获取搜索的消费种类列表
    @Override
    public CommonResponse<List<ExpenseClass>> get_select_expenseclass(String Keyword) {
        QueryWrapper<ExpenseClass> expenseClassQueryWrapper = new QueryWrapper<>();
        expenseClassQueryWrapper.eq("ExpCode" , Keyword);
        List<ExpenseClass> expenseClasses1 = expenseClassMapper.selectList(expenseClassQueryWrapper);
        QueryWrapper<ExpenseClass> expenseClassQueryWrapper1 = new QueryWrapper<>();
        expenseClassQueryWrapper1.eq("ExpName" , Keyword);
        List<ExpenseClass> expenseClasses = expenseClassMapper.selectList(expenseClassQueryWrapper1);
        if(expenseClasses1.isEmpty()&&!expenseClasses.isEmpty()){
            return CommonResponse.createForSuccess("查询成功" , expenseClasses);
        }else if(!expenseClasses1.isEmpty()&&expenseClasses.isEmpty()){
            return CommonResponse.createForSuccess("查询成功" , expenseClasses1);
        }else return CommonResponse.createForError("查询失败");
    }

    //新增一条类别
    @Override
      public CommonResponse<ExpenseClass> insert_new_expenseclass(String ExpCode , String ExpName) {
        ExpenseClass new_expenseclass = new ExpenseClass();
          new_expenseclass.setExpName(ExpName);
          new_expenseclass.setExpCode(ExpCode);
          new_expenseclass.setDelMark(1);
          expenseClassMapper.insert(new_expenseclass);
        return CommonResponse.createForSuccess("插入成功", new_expenseclass);
    }



    @Override
    public CommonResponse<ExpenseClass> update_expenseclass(int id , String ExpCode, String ExpName) {
        ExpenseClass expenseClass = new ExpenseClass();
        expenseClass = expenseClassMapper.selectById(id);
        expenseClass.setExpCode(ExpCode);
        expenseClass.setExpName(ExpName);
        expenseClassMapper.updateById(expenseClass);
        return CommonResponse.createForSuccess("更新成功" , expenseClass);
    }

    //删除
    public CommonResponse<String> delete_expenseclass(Integer id){
       QueryWrapper<ExpenseClass> queryWrapper = new QueryWrapper<>();
       queryWrapper.eq("ID" , id);
       ExpenseClass expenseClass = expenseClassMapper.selectOne(queryWrapper);
       expenseClassMapper.delete(queryWrapper);
       return CommonResponse.createForSuccess("删除成功");
    }

}
